import json

from alibabacloud_chatbot20171011 import models as chatbot_20171011_models

from core.base import Client
from django.conf import settings


class HttpRequest:
    client = Client()

    def execute(self, execute_name, request):
        _execute = getattr(self.client, execute_name)
        response = _execute(request)
        return json.loads(response)


class CategoryRequest(HttpRequest):
    def get_list(self, parent_category_id, show_childrens):
        execute_name = 'query_categories'
        request_obj = chatbot_20171011_models.QueryCategoriesRequest()
        if parent_category_id:
            if isinstance(parent_category_id, int):
                parent_category_id = str(parent_category_id)
            if not isinstance(parent_category_id, str) or parent_category_id.isdigit():
                return
            request_obj.parent_category_id = parent_category_id
        if show_childrens:
            request_obj.show_childrens = show_childrens
        return self.execute(execute_name=execute_name, request=request_obj)

    def get_describe(self, category_id):
        action_name = 'DescribeCategory'
        if category_id:
            if isinstance(category_id, int):
                parent_category_id = str(category_id)
            if not isinstance(parent_category_id, str) or parent_category_id.isdigit():
                return

        return self._execute(action_name=action_name)

    def create(self, name, parent_category_id):
        action_name = 'CreateCategory'
        params = {}
        if not name:
            return
        if parent_category_id:
            if isinstance(parent_category_id, int):
                parent_category_id = str(parent_category_id)
            if not isinstance(parent_category_id, str) or parent_category_id.isdigit():
                return
            params.update(ParentCategoryId=parent_category_id)
        return self._execute(action_name=action_name, query_params=params)


class KnowledgeRequest(HttpRequest):
    def create(self):
        pass
